package com.atguigu.gmall.order.api;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.order.model.OrderInfo;
import com.atguigu.gmall.order.service.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author: atguigu
 * @create: 2023-05-06 10:34
 */
@RestController
@SuppressWarnings("all")
@RequestMapping("/api/order")
public class OrderApiController {

    @Autowired
    private OrderInfoService orderInfoService;


    /**
     * 汇总渲染订单确认页面所选的数据
     *
     * @return
     */
    @GetMapping("/auth/trade")
    public Result orderTrade(HttpServletRequest request) {
        //1.获取当前登录用户ID
        String userId = AuthContextHolder.getUserId(request);
        //2.调用业务逻辑汇总订单确认数据
        Map<String, Object> mapResult = orderInfoService.trade(Long.valueOf(userId));
        return Result.ok(mapResult);
    }


    /**
     * 根据订单ID查询订单信息 包含订单明细
     *
     * @param orderId
     * @return
     */
    @GetMapping("/inner/getOrderInfo/{orderId}")
    public OrderInfo getOrderInfoById(@PathVariable("orderId") Long orderId) {
        return orderInfoService.getOrderInfoById(orderId);
    }


    /**
     * 提供给库存系统进行拆单业务接口
     *
     * @param orderId    订单ID
     * @param wareSkuMap 库存系统查询到仓库ID跟商品对照关系 "[{"wareId":"1","skuIds":["41"]},{"wareId":"2","skuIds":["26"]}]"
     * @return 响应拆单后结果-包含每个订单对应出货仓库ID
     */
    @PostMapping("/orderSplit")
    public String orderSplit(@RequestParam("orderId") String orderId, @RequestParam("wareSkuMap") String wareSkuMap) {
        return orderInfoService.orderSplit(orderId, wareSkuMap);
    }


    /**
     * 提供给秒杀服务 保存秒杀订单接口
     * @param orderInfo
     * @return
     */
    @PostMapping("/inner/seckill/submitOrder")
    public Long submitSeckillOrder(@RequestBody OrderInfo orderInfo){
        return orderInfoService.submitSeckillOrder(orderInfo);
    }


}
